
package ro.msg.quiz.repository;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import ro.msg.quiz.Model.IQuizModel;
import ro.msg.quiz.Model.QuizModel;




/**
 *
 * @author Alexandra

 * This class exports java object to xml file

 */


public class JaxbJavaToXML {

	private RepositoryQuestion questions = new RepositoryQuestion();
	private RepositoryQuiz quizzes = new RepositoryQuiz();
	private IQuizModel relations = new QuizModel();
	
	/**
	 * Constructor
	 */
	public JaxbJavaToXML(RepositoryQuestion question, RepositoryQuiz quizzes,
			IQuizModel relations) {
		exportQuestionsToXML(question);
		exportQuizToXML(quizzes);
		exportRelationQuizQuestion(relations);
	}
	
	/**
	 * This method exports questions to xml file
	 */
	public void exportQuestionsToXML(RepositoryQuestion questions){

		try {
			File XMLfile = new File("./ExportQuestions.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(SingleChoiceQ.class, 
            			MultipleChoiceQ.class, TextQ.class, RepositoryQuestion.class);
	        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            jaxbMarshaller.marshal(questions, XMLfile);
            jaxbMarshaller.marshal(questions, System.out);
            }

        catch (JAXBException e){
            // some exception occured
            e.printStackTrace();
        }

	}

	/**
	 * This method exports quizzes to xml file
	 */
	public void exportQuizToXML( RepositoryQuiz quizzes){

		try {

			File XMLfile = new File("./ExportQuizzes.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Quiz.class, RepositoryQuiz.class);
	        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            jaxbMarshaller.marshal(quizzes, XMLfile);
            jaxbMarshaller.marshal(quizzes, System.out);
        }

        catch (JAXBException e) {
            // some exception occured
            e.printStackTrace();
        }
     }

	/**
	 * This method exports relations between quizzes and questions to xml file
	 */
	public void exportRelationQuizQuestion(IQuizModel relations) {

		try {
			File XMLfile = new File("./ExportRelations.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(QuizModel.class, 
            			Question.class, Quiz.class);
	        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            jaxbMarshaller.marshal(relations, XMLfile);
            jaxbMarshaller.marshal(relations, System.out);
        }

        catch (JAXBException e) {
            // some exception occured
            e.printStackTrace();
        }
	}
}




